
/*========================================================================================
 *                                                                                      *
 *               Copyright(c) 2013-2014, Cha SangWoo, All rights reserved.              *
 *                                                                                      *
 * This file is part of ChaOS.                                                          *
 *                                                                                      *
 * Permission to use, copy, modify, and distribute this software in source and binary   *
 * forms and its documentation for any purpose and without fee is hereby granted,       *
 * provided that the above copyright notice appear in all copies and that both that     *
 * copyright notice and this permission notice appear in supporting documentation.      *
 *                                                                                      *
 * THIS SOFTWARE IS PROVIDED BY THE CHA SANGWOO AND CONTRIBUTORS "AS IS" AND ANY        *
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED            *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
 * IN NO EVENT SHALL CHA SANGWOO OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,    *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT         *
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,          *
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF            *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE      *
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,                    *
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                   *
 *                                                                                      *
========================================================================================*/

#include "co_core.h"
#include "co_mutex.h"

#if (FLAG_USE_CHAOS_MUTEX == CO_ENABLE)

#ifdef __cplusplus
    namespace ChaOS {
#endif

//////////////////////////////////////  < BEGIN >  ///////////////////////////////////////

/*======================================================================================*/
/*!
    @fn             OBJECT co_mutex_create(VOID)

    @brief          Create a mutex

    @return         Object for mutex

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
OBJECT co_mutex_create(VOID)
{
    MUTEX_CB     *pmutex;

    // allocate heap memory
    pmutex = (MUTEX_CB*) co_heap_alloc(sizeof(MUTEX_CB));
    CO_ASSERT(pmutex != NULL);
    
    // set object ID
    OBJID_SET(pmutex, OBJ_MUTEX_ID);

    // initialize
    pmutex->powner = NULL;

    return (OBJECT) pmutex;
}


/*======================================================================================*/
/*!
    @fn             VOID co_mutex_enter(OBJECT object)

    @brief          Enter critical area (get a mutex)

    @param [in]     object - Object for mutex

    @return         None

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
VOID co_mutex_enter(OBJECT object)
{
    MUTEX_CB   *pmutex = (MUTEX_CB*) object;

    // check object ID
    OBJID_CHECK(pmutex, OBJ_MUTEX_ID);

    ///////////////////
    CO_ENTER_CRITICAL();

    // Do not preocess in ISR
    if (_g_nested_count > CO_NESTED_COUNT_USER)
    {
        CO_LEAVE_CRITICAL();
        ////////////////////

        return;
    }
    // check mutex
    else if (!pmutex->powner)
    {
        pmutex->powner = GET_CURRENT_THREAD();

        CO_LEAVE_CRITICAL();
        ////////////////////

        return;
    }
    // wait mutex
    else
    {
        THREAD_CB   *pthread = GET_CURRENT_THREAD();

        pthread->state = CO_THREAD_WAIT_OBJECT;
        pthread->wait_object = object;

        _core_add_block_list(pthread);

        CO_LEAVE_CRITICAL();
        ////////////////////

        PORT_SWITCH_IN_THREAD();

        return;
    }
}


/*======================================================================================*/
/*!
    @fn             VOID co_mutex_leave(OBJECT object)

    @brief          Leave critical area (give a mutex).

    @param [in]     object - Object for mutex

    @return         None

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
VOID co_mutex_leave(OBJECT object)
{
    MUTEX_CB   *pmutex = (MUTEX_CB*) object;

    // check object ID
    OBJID_CHECK(pmutex, OBJ_MUTEX_ID);

    ///////////////////
    CO_ENTER_CRITICAL();

    CO_ASSERT(pmutex->powner == GET_CURRENT_THREAD());

    // Do not preocess in ISR
    if (_g_nested_count > CO_NESTED_COUNT_USER)
    {
        CO_LEAVE_CRITICAL();
        ////////////////////

        return;
    }

    // search thread waiting object
    pmutex->powner = _core_search_thread_in_block_list(object);

    CO_LEAVE_CRITICAL();
    ////////////////////
}


//////////////////////////////////////  <  END  >  ///////////////////////////////////////

#ifdef __cplusplus
    } /* namespace ChaOS */
#endif

#endif // (FLAG_USE_CHAOS_MUTEX == CO_ENABLE)

